﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using EPiServer.Licensing;
using EPiServer.DataAbstraction;
using EPiServer.Data.Dynamic;
using Auros.EPiSiteMap.Data;

namespace Auros.EPiSiteMap.WebControls
{
    public class SiteMapTable : Table
    {
        public string SiteID { get; set; }

        IEnumerable<SiteMapHostConfig> StoreItems { get; set; }

        DynamicDataStore Store { get; set; }

        public SiteMapTable()
        {
            this.Store = DynamicDataStoreFactory.Instance.GetStore(typeof(SiteMapHostConfig))
                        ?? DynamicDataStoreFactory.Instance.CreateStore(typeof(SiteMapHostConfig));

            this.StoreItems = this.Store.LoadAll<SiteMapHostConfig>();
        }

        protected override void OnLoad(EventArgs e)
        {
            var hosts = SiteInformationHandler.GetSiteInformation(this.SiteID, true);

            this.CreateHeaderRow();

            foreach (var host in hosts.SiteHosts.OrderBy(item => item.SiteHost))
            {
                var sitemapHostData = this.StoreItems.FirstOrDefault(item => item.SiteID == this.SiteID && item.Host == host.SiteHost);

                SiteMapTableRow row;

                if (sitemapHostData != null)
                {
                    row = new SiteMapTableRow(this.SiteID, host, sitemapHostData);
                }
                else
                {
                    row = new SiteMapTableRow(this.SiteID, host);
                }
                
                this.Rows.Add(row);
            }


            base.OnLoad(e);
        }

        private void CreateHeaderRow()
        {
            TableRow row = new TableRow();
            row.TableSection = TableRowSection.TableHeader;

            row.Cells.Add(new TableHeaderCell() { Text = "Create" });
            row.Cells.Add(new TableHeaderCell() { Text = "Host", Width = new Unit(100, UnitType.Percentage) });

            // Do Languages
            foreach (var language in EPiServer.DataAbstraction.LanguageBranch.ListEnabled())
            {
                row.Cells.Add(new TableHeaderCell() { Text = language.LanguageID });
            }

            row.Cells.Add(new TableHeaderCell() { Text = "File name" });

            foreach (TableHeaderCell cell in row.Cells)
            {
                cell.Attributes.Add("scope", "col");
            }

            this.Rows.Add(row);
        }
    
        public void Save()
        {
            foreach (TableRow row in this.Rows)
            {
                var typedRow = row as SiteMapTableRow;

                if (typedRow != null)
                {
                    var data = this.StoreItems.FirstOrDefault(item => item.SiteID == this.SiteID && item.Host == typedRow.HostSettings.SiteHost);

                    if (data == null)
                    {
                        data = new SiteMapHostConfig();
                    }

                    data.Enabled = typedRow.SitemapEnabledCheckbox.Checked;
                    data.SiteID = this.SiteID;
                    data.Host = typedRow.HostSettings.SiteHost;
                    data.FileName = typedRow.FileNameTextBox.Text;
                    data.Langauges = typedRow.LanguageCheckBoxes.Where(item => item.Checked && item.Enabled).Select(item => item.Value).ToArray();

                    this.Store.Save(data);
                }
            }
        }
    }
}